<?

function getDBConnection(){
	$user = "root"; // Username für Datenbank-Login
	$pass = "taketo24"; // Passwort für Datenbank-Login
	$host = "localhost"; // Adresse zur Datenbank
	$db = "sommer"; // Name der Datenbank
	// DB-Connect
	$sql_con = mysql_connect( $host, $user, $pass);
	mysql_select_db($db, $sql_con);
	return $sql_con;
}

function doQuery($query,$limit = -1){

	//get all hoert entries from db for the given userid
	$db = getDBConnection();
	$result = mysql_query($query);
	if($result === FALSE){
		trigger_error("Datenbankfehler.",E_WARNING);
		echo mysql_error();
		return array();
	}
	
	$err = mysql_error();
	if($err != "" && $err != null)
	{
		trigger_error($err);
		return array();
	}
	
	$array = array();
	$element = "";
	for($i = 0; $i < $limit || $limit == -1;$i++){
		$element = mysql_fetch_assoc($result);
		if($element === FALSE) break;
		$array[$i] = $element;
	}
	return $array;
}

function doNonSelectQuery($query){
	$db = getDBConnection();
	$result = mysql_query($query);
	//true on success otherwise false;
	$err = mysql_error();
	if($err == "" || $err == null)
	return $result;
	else {
		trigger_error($err);
		return false;
	}
	
}



function db_get_veranstaltungen(){

	$db = "SELECT id as ID, title as Titel, is_vorlesung as Art,(select count(0) from `haelt` where veranstaltung.id = haelt.ver_id  AND haelt.toDate > CURRENT_DATE()) AS Geplant from veranstaltung ORDER BY Geplant DESC,Art,ID,Titel";
	return doQuery($db);
}


function db_get_dozenten(){

	$db = "SELECT id as ID, name as Name, fachbereich as Fachbereich,(select count(0) from `haelt` where dozent.id = haelt.doz_id  AND haelt.toDate > CURRENT_DATE()) AS Veranstaltungen from dozent ORDER BY Veranstaltungen DESC,Name,ID";
	return doQuery($db);
}

function db_get_veranstaltung($id){
	$db = "SELECT * from veranstaltung WHERE id = '$id' LIMIT 1";
	return doQuery($db,1);
}

function db_get_dozent($id){
	$db = "SELECT * from dozent WHERE id = '$id' LIMIT 1";
	return doQuery($db,1);
}

function db_get_haelt($ver_id){	
	$db = "SELECT * from haelt WHERE ver_id = '$ver_id'";
	return doQuery($db);	
}

/**
 *
 *
 * @var array(id,name,sprechstunde,email,homepage,fachbereich)
 **/
function db_set_dozent($data){
	$query = "";
	$mode = "";
	$end = "";
	if($data['id'] == "") $mode = "INSERT INTO";
	else{
		$mode = "UPDATE";
		$end = "WHERE id = '".$data['id']."'";
	}

	$query = sprintf("%s dozent SET
	name='%s',
	sprechstunde='%s',
	email='%s',
	homepage='%s',
	fachbereich='%s' %s;
	",
	$mode,
	$data['name'],
	$data['sprechstunde'],
	$data['email'],
	$data['homepage'],
	$data['fachbereich'],
	$end);
	return doNonSelectQuery($query);

}

/**
 *
 *
 * @var array(id,fachbereich,title,content,is_vorlesung,url)
 **/
function db_set_veranstaltung($data){
	$query = "";
	$mode = "";
	$end = "";
	if($data['id'] == "") $mode = "INSERT INTO";
	else{
		$mode = "UPDATE";
		$end = "WHERE id = '".$data['id']."'";
	}

	$query = sprintf("%s veranstaltung SET
	fachbereich='%s',
	title='%s',
	content='%s',
	is_vorlesung='%s',
	url='%s' %s;
	",
	$mode,
	$data['fachbereich'],
	$data['title'],
	$data['content'],
	$data['is_vorlesung'],
	$data['url'],
	$end);
	return doNonSelectQuery($query);

}
/**
 *
 *
 * @var array(id,ver_id,doz_id,cost,max_subscriber,place,fromDate,toDate,fromTime,toTime)
 **/
function db_set_haelt($data){
	
	$data['id'] = $_POST['id'];
	$data['ver_id'] = $_POST['ver_id'];
	$data['doz_id'] = $_POST['doz_id'];
	$data['cost'] = $_POST['cost'];
	$data['max_subscriber'] = $_POST['max_subscriber'];
	$data['place'] = $_POST['place'];
	$data['fromDate'] = $_POST['fromDate'];
	$data['toDate'] = $_POST['toDate'];
	$data['fromTime'] = $_POST['fromTime'];
	$data['toTime'] = $_POST['toTime'];
	$query = "";
	$mode = "";
	$end = "";
	if($data['id'] == "") $mode = "INSERT INTO";
	else{
		$mode = "UPDATE";
		$end = "WHERE id = '".$data['id']."'";
	}

	$query = sprintf("%s haelt SET
	ver_id='%s',
	doz_id='%s',
	cost='%s',
	max_subscriber='%s',
	place='%s',
	fromDate='%s',
	toDate='%s',
	fromTime='%s',
	toTime='%s' %s;
	",
	$mode,
	$data['ver_id'],
	$data['doz_id'],
	$data['cost'],
	$data['max_subscriber'],
	$data['place'],
	$data['fromDate'],
	$data['toDate'],
	$data['fromTime'],
	$data['toTime'],
	$end);
	return doNonSelectQuery($query);

}

?>